<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ActivityLogResource\Pages;
use App\Models\ActivityLog;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Tables\Actions\ViewAction;

class ActivityLogResource extends Resource
{
    protected static ?string $model = ActivityLog::class;

    protected static ?string $navigationIcon = 'heroicon-o-document-text';

    protected static ?int $navigationSort = 2;

    // 导航项分组翻译
    public static function getNavigationGroup(): ?string
    {
        return __('menu.power_group.name');
    }

    // 菜单标题导航翻译
    public static function getNavigationLabel(): string
    {
        return __('menu.power_group.activity_log');
    }

    // 面包屑导航翻译
    public static function getModelLabel(): string
    {
        return __('menu.power_group.activity_log');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Textarea::make('properties')
                    ->label(__('activity-log.Properties'))
                    ->rows(10)
                    ->readOnly(),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('id')->label(__('activity-log.Id')),
                TextColumn::make('causer.name')
                    ->label(__('activity-log.Causer_id'))
                    ->listWithLineBreaks()
                    ->badge()
                    ->separator()
                    ->alignCenter(),
                TextColumn::make('description')
                    ->label(__('activity-log.Description'))
                    ->formatStateUsing(fn(string $state): string => match ($state) {
                        'created'         => '创建',
                        'updated'         => '修改',
                        'deleted'         => '删除',
                        'login succeeded' => '登录成功',
                    })
                    ->color('success')
                    ->alignCenter(),
                TextColumn::make('ip')
                    ->label(__('activity-log.Ip'))
                    ->color('info')
                    ->copyable()
                    ->alignCenter(),
                TextColumn::make('created_at')
                    ->label(__('activity-log.Created_at'))
                    ->alignCenter(),
            ])
            ->filters([
                //
            ])
            ->actions([
                ViewAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('id', 'desc');
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListActivityLogs::route('/'),
        ];
    }
}
